﻿#include "FITKNastranAdaptorCase.h"
#include "FITKNastranBDFIOInterface.h"
#include "FITKNastranBDFReader.h"
#include "InpNastranReaderHelper.h"

#include "FITK_Component/FITKAbaqusData/FITKDataCase.h"
#include "FITK_Interface/FITKInterfaceModel/FITKComponentManager.h"
#include "FITK_Interface/FITKInterfacePhysics/FITKSectionAssign.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterial.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterialBehavior.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterialDamping.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterialDensity.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterialElastic.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterialExpansion.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMeshPartVTK.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSectionBeam.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSectionShellHomogeneous.h"
#include "FITK_Kernel/FITKAdaptor/FITKIOAdaptorFactory.h"
#include "FITK_Kernel/FITKAppFramework/FITKMessage.h"

#define VIEWWINDOWSIZE 20

namespace Nastran
{
FITKNastranBDFReader::FITKNastranBDFReader(FITKNastranBDFIOInterface *compInterface, bool &success)
    : _isSuccess(success), _compInterface(compInterface)
{
    if (nullptr == _compInterface)
        return;
    _caseData = _compInterface->getDataObjectT<AbaqusData::FITKDataCase>("Model");
}

// Entry point for the FITKNastranBDFReader class, executing the main process
void FITKNastranBDFReader::run()
{
    _isSuccess = false;
    // Check if _caseData is null or if the file cannot be loaded, if so, exit the function
    if (nullptr == _caseData || !(this->loadFile()))
    {
        return;
    }
    // Create an instance of InpNastranReaderHelper with _caseData
    InpNastranReaderHelper helper(_caseData);
    // Set the reader for the helper to this instance
    helper.setReader(this);
    // Declare a temporary FITKDataCase object
    AbaqusData::FITKDataCase tempCaseData;
    // Create an adaptor for Nastran using the factory with the temporary case data
    auto adaptor = FITKIOADAPTORFACTORY->createT<Nastran::FITKNastranAdaptorCase>("BDF", &tempCaseData);
    // Check if the adaptor is null, if so, log an error message
    if (nullptr == adaptor)
    {
        consoleMessage(3, "null ptr error for FITKNastranAdaptorCase");
    }
    else
    {
        // If the adaptor is not null, set the helper, reader, and case data
        adaptor->setReaderHelper(&helper);
        adaptor->setReader(this);
        adaptor->setCaseData(_caseData);
        // Attempt to adapt the data, if it fails, log an error message
        _isSuccess = adaptor->adaptR();
        if (!_isSuccess)
        {
            consoleMessage(3, "error adapting for case");
        }
    }
    // Delete the adaptor to free memory
    delete adaptor;
}

void FITKNastranBDFReader::consoleMessage(int level, const QString &str)
{
    switch (level)
    {
    case 1:
        AppFrame::FITKMessageNormal(str);
        break;
    case 2:
        AppFrame::FITKMessageWarning(str);
        break;
    case 3:
        AppFrame::FITKMessageError(str);
        break;
    default:
        AppFrame::FITKMessageError(str);
        break;
    }
}

} // namespace Nastran
